<?php

namespace common\models;

use Yii;
use common\models\MktPost;

/**
 * This is the model class for table "mkt_post_response".
 *
 * @property string $id
 * @property string $post_id
 * @property string $user_id
 * @property string $content
 * @property integer $is_read
 * @property integer $is_done
 * @property integer $is_deleted
 * @property string $create_time
 */
class MktPostResponse extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_post_response';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['post_id', 'user_id', 'is_read', 'is_done', 'is_deleted'], 'integer'],
            [['create_time'], 'safe'],
            [['content'], 'string', 'max' => 1024]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'post_id' => '主贴ID',
            'user_id' => '用户ID',
            'content' => '正文',
            'is_read' => '此回复已被贴主阅',
            'is_done' => '此回复已被贴主处理',              // 忽略标志?
            'is_deleted' => '删除标志',
            'create_time' => '创建时间',
        ];
    }

    /**
     * @inheritdoc
     * @return MktPostResponseQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktPostResponseQuery(get_called_class());
    }

    public function getPost()
    {
        return $this->hasOne(MktPost::className(), ['id' => 'post_id']);
    }

    public function getUser()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'user_id']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($insert) {
            if (MktPost::POST_TYPE_JOB == $this->post->post_type)
                \common\models\WxTemplateId::sendJobPostResponse($this);                    
            else
                \common\models\WxTemplateId::sendCooperatePostResponse($this);                                
        }
    }

    public static function ignorePostResponseAjax($params)
    {
        $model = MktPostResponse::findOne($params['id']);
        $model->is_done = 1;
        $model->is_read = 1;
        $result = $model->save(false);
        if ( true !== $result) {
            yii::error(print_r($model->getErrors(), true));
            return \yii\helpers\Json::encode(['code' => 1, 'data' => 'save error']);
        }
        return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
    }

    public static function addPostResponseAjax($params)
    {
        $model = new MktPostResponse();
        $model->user_id = $params['user_id'];
        $model->post_id = $params['post_id'];

        $user = MktUser::findOne($params['user_id']);

        if ($user->postResponseCountToday > Yii::$app->keyStorage->get('business.post_response_per_day', 5)) {      
            yii::error('postResponseCountToday=' . $user->postResponseCountToday);
            return \yii\helpers\Json::encode(['code' => 1, 'msg' => '每天限发送应聘及合作请求' . Yii::$app->keyStorage->get('business.post_response_per_day', 5) . '次']);
        }
        
        $post = MktPost::findOne($params['post_id']);
        if ($post->post_type == MktPost::POST_TYPE_COOPERATION)            
            $model->content = '我想合作';        
        else if ($post->post_type == MktPost::POST_TYPE_JOB)    
            $model->content = '我想应聘';                
            
        if ( !$model->save(true) ) {
            yii::error(print_r($model->getErrors(), true));
            return \yii\helpers\Json::encode(['code' => 1, 'msg' => 'save error']);
        }
        return \yii\helpers\Json::encode(['code' => 0, 'msg' => 'OK']);
    }

}
